/********************************************************************************************
** Copyright � 2011 Nokia Corporation. All rights reserved.
** Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
** Java and all Java-based marks are trademarks or registered trademarks of
** Sun Microsystems, Inc. Other product and company names mentioned herein may be
** trademarks or trade names of their respective owners.
**
** Subject to the conditions below, you may, without charge:
**
** �  Use, copy, modify and/or merge copies of this software and
**    associated content and documentation files (the �Software�)
**
** �  Publish, distribute, sub-licence and/or sell new software
**    derived from or incorporating the Software.
**
** Some of the documentation, content and/or software maybe licensed under open source
** software or other licenses.  To the extent such documentation, content and/or
** software are included, licenses and/or other terms and conditions shall apply
** in addition and/or instead of this notice. The exact terms of the licenses, disclaimers,
** acknowledgements and notices are reproduced in the materials provided.
**
** This file, unmodified, shall be included with all copies or substantial portions
** of the Software that are distributed in source code form.
**
** The Software cannot constitute the primary value of any new software derived
** from or incorporating the Software.
**
** Any person dealing with the Software shall not misrepresent the source of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
** INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
** PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
** HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
********************************************************************************************/
import Qt 4.7

import "DCDataClasses.js" as DCDATA

Rectangle {
    id: routeselectiondlg
    width: 300
    height: 230
    gradient: Gradient {
        GradientStop {
            position: 0.44
            color: "#abd1ee"
        }

        GradientStop {
            position: 1
            color: "#000000"
        }
    }
    z: 50

    property string datacloudurl: ""
    property string username: ""
    property string password: ""
    property string karpoid: ""
    property string routekarpoid: "0"


    signal opened()

    function getRouteKarpoId()
    {
        var ind = routecombobox.selectedind
        if (ind < 0 || ind > loadedroutes.count)
        {
            return ""
        }

        return loadedroutes.get(ind).routekarpoid;
    }


    function getRouteOwnerkarpoid()
    {
        var ind = routecombobox.selectedind
        if (ind < 0 || ind > loadedroutes.count)
        {
            return ""
        }

        return loadedroutes.get(ind).ownerkarpoid;
    }

    function getRouteName()
    {
        var ind = routecombobox.selectedind
        if (ind < 0 || ind > loadedroutes.count)
        {
            return ""
        }

        return loadedroutes.get(ind).name;
    }

    function getRouteDescription()
    {
        var ind = routecombobox.selectedind
        if (ind < 0 || ind > loadedroutes.count)
        {
            return ""
        }

        return loadedroutes.get(ind).description;
    }

    function getRouteTime()
    {
        var ind = routecombobox.selectedind
        if (ind < 0 || ind > loadedroutes.count)
        {
            return ""
        }

        return loadedroutes.get(ind).time;
    }

    function getRouteRepeat()
    {
        var ind = routecombobox.selectedind
        if (ind < 0 || ind > loadedroutes.count)
        {
            return ""
        }

        return loadedroutes.get(ind).repeat;
    }

    function getRouteFrom()
    {
        var ind = routecombobox.selectedind
        if (ind < 0 || ind > loadedroutes.count)
        {
            return ""
        }

        var routepoints = JSON.parse(loadedroutes.get(ind).route)
        if (routepoints[0].type == "FROMPOINT")
        {
            return routepoints[0]
        }
        else
        {
            return routepoints[1]
        }
    }

    function getRouteTo()
    {
        var ind = routecombobox.selectedind
        if (ind < 0 || ind > loadedroutes.count)
        {
            return ""
        }

        var routepoints = JSON.parse(loadedroutes.get(ind).route)
        if (routepoints[0].type == "TOPOINT")
        {
            return routepoints[0]
        }
        else
        {
            return routepoints[1]
        }
    }

    function getRouteShowownername()
    {
        var ind = routecombobox.selectedind
        if (ind < 0 || ind > loadedroutes.count)
        {
            return ""
        }

        return loadedroutes.get(ind).showownername;
    }

    function getRouteShowownerhometown()
    {
        var ind = routecombobox.selectedind
        if (ind < 0 || ind > loadedroutes.count)
        {
            return ""
        }

        return loadedroutes.get(ind).showownerhometown;
    }

    function getRouteShowownerphone()
    {
        var ind = routecombobox.selectedind
        if (ind < 0 || ind > loadedroutes.count)
        {
            return ""
        }

        return loadedroutes.get(ind).showownerphone;
    }

    function getRouteCarpoolkarpoid()
    {
        var ind = routecombobox.selectedind
        if (ind < 0 || ind > loadedroutes.count)
        {
            return ""
        }

        return loadedroutes.get(ind).carpoolkarpoid;
    }

    function openroutescb(result)
    {
        console.log("getroutescb: " + result.statusString)

        loadedroutes.clear()

        if (!result.isError)
        {
            var routearray = []
            for (var i = 0; i < result.routelist.length; i++)
            {
                var routeitem = result.routelist[i]
                routeitem.route = JSON.stringify(routeitem.route)
                routeitem.ownerinfo = JSON.stringify(routeitem.ownerinfo)
                loadedroutes.append(routeitem)

                routearray[i] = routeitem.name

            }

            routecombobox.setComboItems(routearray)
            if (loadedroutes.count > 0)
            {
                routecombobox.setSelectedItem(routearray[0])
                routecombobox.selectedind = 0
                infoid.text = loadedroutes.get(0).description
            }

            messagebox.visible = false
        }
        else
        {
            messagebox.text = "Failed: " + result.errorString
        }

    }


    function deleteroutecb(result)
    {
        try
        {
            console.log("DELETE ROUTE: " + result.statusString)

            if (!result.isError)
            {
                // update the route list
                routecombobox.deleteComboItem(routecombobox.selectedind)
                messagebox.text = "Route is deleted"
            }
            else
            {
                messagebox.text = "Failed: " + result.errorString
                messagebox.show("")
            }
        }
        catch (ex)
        {
            messagebox.text = ex
            messagebox.show("")
        }

    }



    function deleteSelectedRoute()
    {

        messagebox.text = "Deleting route..."
        messagebox.show("")

        var dcroute = new DCDATA.DCRoute();
        dcroute.routekarpoid = getRouteKarpoId()
        dcroute.carpoolkarpoid = getRouteCarpoolkarpoid()
        dcroute.ownerkarpoid = getRouteOwnerkarpoid()
        dcroute.karpoid = dcroute.ownerkarpoid
        dcroute.password = password

        DCDATA.send(dcroute, datacloudurl, "deleteroute", deleteroutecb)

    }


    ListModel
    {
        id: loadedroutes

    }


    function loadRoutes(enableDeleting)
    {
        messagebox.text = "Loading the routes from the DataCloud..."
        messagebox.show("")

        routecombobox.deletebutton = enableDeleting

        var dcroutelist = new DCDATA.DCRouteList()
        dcroutelist.karpoid = karpoid
        dcroutelist.password = password

        DCDATA.send(dcroutelist, datacloudurl, "loadroutes", openroutescb)
    }

    Rectangle
    {
        id: titlerect
        width: 110
        height: 29
        color: "#5d8eac"
        anchors.right: parent.right
        anchors.rightMargin: 0
        anchors.left: parent.left
        anchors.leftMargin: 0
        anchors.top: parent.top
        anchors.topMargin: 0

        Text {
            id: titleid
            width: 110
            height: 29
            text: "Route selection"
            font.family: "Arial"
            anchors.leftMargin: 10
            anchors.fill: parent
            anchors.verticalCenter: titlerect.verticalCenter
            font.pixelSize: 16
        }

        MouseArea {
            id: titlerectmousearea
            anchors.fill: parent

            drag.target: routeselectiondlg
            drag.axis: Drag.XandYAxis
            drag.minimumX: 0
            drag.maximumX: 1000
            drag.minimumY: 0
            drag.maximumY: 1000
        }
    }

    property int labelwidth: 90
    property int labelfontpixesize: 14
    property int inputboxheight: 26


    Text {
        id: routeid
        text: "Select route:"
        anchors.top:  titlerect.bottom
        anchors.topMargin: 10
        anchors.left: parent.left
        anchors.leftMargin: 10
        anchors.right: parent.right
        anchors.rightMargin: 10
        font.pixelSize: labelfontpixesize
    }

    ComboBox {
        id: routecombobox
        deletebutton: true
        height: routeselectiondlg.inputboxheight
        anchors.top:  routeid.bottom
        anchors.topMargin: 10
        anchors.left: parent.left
        anchors.leftMargin: 10
        anchors.right: parent.right
        anchors.rightMargin: 10


        onItemSelected:
        {
            infoid.text = loadedroutes.get(routecombobox.selectedind).description
        }

        onItemDelete:
        {
            if (label == "")
            {
                messagebox.text = "Nothing selected"
                messagebox.show("")
            }
            else
            {
                messagebox.text = "Do you want to delete<br>" + label
                messagebox.onOkTodo = "deleteSelectedRoute"
                messagebox.show("confirmation")
            }
        }

    }

    Text {
        id: infoid
        text: ""
        anchors.top:  routecombobox.bottom
        anchors.topMargin: 10
        anchors.left: parent.left
        anchors.leftMargin: 10
        anchors.right: parent.right
        anchors.rightMargin: 10
        anchors.bottom: openid.top
        wrapMode:  Text.WordWrap
        font.pixelSize: labelfontpixesize
    }

    PushButton
    {
        id: openid
        label: "Open"
        width: 80
        anchors.right: parent.right
        anchors.rightMargin: 10
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 10


        onButtonpressed:
        {
            if (routecombobox.selectedind < 0)
            {
                messagebox.text = "No route selected."
                messagebox.show()
            }
            else
            {
                routeselectiondlg.opened()
            }
        }

    }

    PushButton
    {
        id: cancelid
        label: "Cancel"
        width: 80
        anchors.right: openid.left
        anchors.rightMargin: 20
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 10

        onButtonpressed:
        {
           routeselectiondlg.visible = false
        }

    }

    MessageBox
    {
        id: messagebox
        property string onOkTodo: "" // deleteSelectedRoute

        onOk:
        {
            if (onOkTodo == "deleteSelectedRoute")
            {
                deleteSelectedRoute()
            }

            onOkTodo = ""
        }

        onCancel:
        {
            onOkTodo = ""
        }
    }

}
